//
//  PSScoreTests.m
//  PrefCore
//
//  Created by Sergey on 3/3/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "PSGame.h"
#import "PSScore.h"

@interface PSScoreTests : XCTestCase

@end

@implementation PSScoreTests

- (void)testValues {
    PSScoreValues *v = [[PSScoreValues alloc] init];
    XCTAssertNotNil(v);
    XCTAssertFalse(v.isChanged);
    XCTAssertEqual(0LL, v.current);
    XCTAssertEqual(0UL, v.history.count);
    [v addValue:2];
    XCTAssertEqual(0UL, v.history.count);
    XCTAssertEqual(2LL, v.current);
    XCTAssertTrue(v.isChanged);
    [v addValue:3];
    XCTAssertEqual(0UL, v.history.count);
    XCTAssertEqual(5LL, v.current);
    [v settle];
    XCTAssertFalse(v.isChanged);
    [v addValue:11];
    XCTAssertTrue(v.isChanged);
    XCTAssertEqual(1UL, v.history.count);
    XCTAssertEqual(16LL, v.current);
    [v settle];
    XCTAssertFalse(v.isChanged);
    [v addValue:3];
    XCTAssertTrue(v.isChanged);
    XCTAssertEqual(2UL, v.history.count);
    XCTAssertEqual(19LL, v.current);
    [v reset];
    XCTAssertFalse(v.isChanged);
    XCTAssertEqual(0LL, v.current);
}

-(void)testCloseable {
    PSClosableValues *v = [[PSClosableValues alloc] init];
    XCTAssertNotNil(v);
    [v addValue:3];
    XCTAssertEqual(3LL, v.current);
    XCTAssertEqual(3LL, v.eventual);
    v.delta = -1;
    XCTAssertEqual(3LL, v.current);
    XCTAssertEqual(2LL, v.eventual);
    [v reset];
    XCTAssertFalse(v.isChanged);
    XCTAssertEqual(0LL, v.current);
    XCTAssertEqual(0LL, v.delta);
}

-(void)testBalance {
    PSScoreBalance *b = [[PSScoreBalance alloc] init];
    XCTAssertNotNil(b);
    XCTAssertEqual(0LL, b.current);
    XCTAssertEqual(0LL, b.delta);
    b.current = 123;
    b.delta = 12;
    XCTAssertEqual(123LL, b.current);
    XCTAssertEqual(12LL, b.delta);
    [b reset];
    XCTAssertEqual(0LL, b.current);
    XCTAssertEqual(0LL, b.delta);
}

-(void)testScoreInit {
    PSScore *s = [[PSScore alloc] init];
    XCTAssertNotNil(s);
    XCTAssertNotNil(s.pool);
    XCTAssertNotNil(s.mountain);
    for (int i = 0 ; i != PS_GAME_PLAYERS ; i++) {
        XCTAssertNotNil(s.whists[i]);
    }
}

-(void)testScoreParts {
    PSScore *s = [[PSScore alloc] init];
    [s.pool addValue:2];
    [s.mountain addValue:-2];
    [s.whists[0] addValue:100];
    [s.whists[1] addValue:120];
    [s.whists[2] addValue:50];
    XCTAssertEqual(2LL, s.pool.current);
    XCTAssertEqual(-2LL, s.mountain.current);
    XCTAssertEqual(100LL, s.whists[0].current);
    XCTAssertEqual(120LL, s.whists[1].current);
    XCTAssertEqual(50LL, s.whists[2].current);
}

-(void)testScoreReset {
    PSScore *s = [[PSScore alloc] init];
    [s.pool addValue:2];
    [s.mountain addValue:-2];
    [s.whists[0] addValue:100];
    [s.whists[1] addValue:120];
    [s.whists[2] addValue:50];
    [s reset];
    XCTAssertEqual(0LL, s.pool.current);
    XCTAssertEqual(0LL, s.mountain.current);
    XCTAssertEqual(0LL, s.whists[0].current);
    XCTAssertEqual(0LL, s.whists[1].current);
    XCTAssertEqual(0LL, s.whists[2].current);
}

-(void)testScoreSettle {
    PSScore *s = [[PSScore alloc] init];
    [s.pool addValue:2];
    [s.mountain addValue:-2];
    [s.whists[0] addValue:100];
    [s.whists[1] addValue:120];
    [s.whists[2] addValue:50];
    XCTAssertTrue(s.pool.isChanged);
    XCTAssertTrue(s.mountain.isChanged);
    for (int i = 0 ; i != PS_GAME_PLAYERS ; i++) {
        XCTAssertTrue(s.whists[i].isChanged);
    }
    [s settle];
    XCTAssertFalse(s.pool.isChanged);
    XCTAssertFalse(s.mountain.isChanged);
    for (int i = 0 ; i != PS_GAME_PLAYERS ; i++) {
        XCTAssertFalse(s.whists[i].isChanged);
    }
}

-(void)testScoreDisplay {
    PSScore *s = [[PSScore alloc] init];
    [s.pool addValue:2];
    [s.mountain addValue:-2];
    [s.whists[0] addValue:100];
    [s.whists[1] addValue:120];
    [s.whists[2] addValue:50];
    XCTAssertEqualObjects(@"[M:-2,P:2,W:100,120,50]", [s description]);
}

@end
